<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表格识别</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            text-align: center;
        }
        .upload-area {
            border: 2px dashed #ccc;
            padding: 20px;
            margin: 20px 0;
            border-radius: 5px;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }
        #imagePreview {
            max-width: 100%;
            margin: 20px 0;
        }
        #tableResult {
            margin-top: 20px;
            overflow-x: auto;
        }
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>表格识别系统</h1>
        <div class="upload-area">
            <input type="file" id="fileInput" accept="image/*">
            <p>请上传包含表格的图片</p>
        </div>
        <img id="imagePreview" style="display: none;">
        <button id="recognizeBtn" class="button" style="display: none;">识别表格</button>
        <div id="tableResult"></div>
    </div>

    <script>
        document.getElementById('fileInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (!file) return;

            // 显示图片预览
            const imagePreview = document.getElementById('imagePreview');
            imagePreview.src = URL.createObjectURL(file);
            imagePreview.style.display = 'block';
            
            // 显示识别按钮
            document.getElementById('recognizeBtn').style.display = 'inline-block';
        });

        document.getElementById('recognizeBtn').addEventListener('click', async function() {
            const file = document.getElementById('fileInput').files[0];
            if (!file) {
                alert('请先选择一个图片文件');
                return;
            }

            // 禁用按钮
            this.disabled = true;
            this.textContent = '正在识别...';

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('/api/recognize_table', {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                if (data.table) {
                    renderTable(data.table);
                } else {
                    alert('识别失败: ' + (data.error || '未知错误'));
                }
            } catch (error) {
                console.error('Error:', error);
                alert('发生错误，请稍后重试');
            } finally {
                // 重新启用按钮
                this.disabled = false;
                this.textContent = '识别表格';
            }
        });

        function renderTable(tableData) {
            const tableResult = document.getElementById('tableResult');
            tableResult.innerHTML = '';
            
            const table = document.createElement('table');
            
            // 创建表头
            const thead = document.createElement('thead');
            const headerRow = document.createElement('tr');
            
            tableData[0].forEach(cell => {
                const th = document.createElement('th');
                th.textContent = cell;
                headerRow.appendChild(th);
            });
            
            thead.appendChild(headerRow);
            table.appendChild(thead);
            
            // 创建表体
            const tbody = document.createElement('tbody');
            
            for (let i = 1; i < tableData.length; i++) {
                const row = document.createElement('tr');
                
                tableData[i].forEach(cell => {
                    const td = document.createElement('td');
                    td.textContent = cell;
                    row.appendChild(td);
                });
                
                tbody.appendChild(row);
            }
            
            table.appendChild(tbody);
            tableResult.appendChild(table);
        }
    </script>
</body>
</html> 